package com.example.luckybag.sysservice

import android.R
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.os.Build
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import androidx.core.app.NotificationCompat
import com.example.luckybag.AccessibilityBusiness
import com.example.luckybag.MainActivity
import com.example.luckybag.service.LuckyBagService


/**
 * 文件名： <br/>
 * 描述：无障碍服务  🌝🌝🌝
 *
 * @author wulinran
 * @since 2023/2/3 13:45
 */
class MyAccessibilityService : AccessibilityService() {

    private var screenWidth = 0

    private val accessibilityBusiness: AccessibilityBusiness by lazy {
        AccessibilityBusiness()
    }
    companion object {
        const val WECHAT_PACKAGE_NAME = "com.tencent.mm"
        const val DING_TALK_PACKAGE_NAME = "com.alibaba.android.rimet"
    }

    override fun onCreate() {
        super.onCreate()
        val ID = "com.example.luckybag" //这里的id里面输入自己的项目的包的路径

        val NAME = "MyAccessibilityService"
        val intent = Intent(this@MyAccessibilityService, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
        val notification: NotificationCompat.Builder //创建服务对象

        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.setShowBadge(true)
            channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            manager.createNotificationChannel(channel)
            notification = NotificationCompat.Builder(this@MyAccessibilityService).setChannelId(ID)
        } else {
            notification = NotificationCompat.Builder(this@MyAccessibilityService)
        }
        notification.setContentTitle("抢红包无障碍")
            .setContentText("瞧好吧您嘞")
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.sym_def_app_icon)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.sym_def_app_icon))
            .setContentIntent(pendingIntent)
            .build()
        val notification1 = notification.build()
        startForeground(1, notification1)
    }


    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        screenWidth = Resources.getSystem().displayMetrics.widthPixels //屏幕宽度
        Log.e("测试无障碍 屏幕宽度", screenWidth.toString())

        //收到消息 直接抛给accessibilityBusiness处理
        // 为什么rootInActiveWindow用接口的形式，而不是直接当入参传，是因为如果直接传，那么在打开微信红包时，
        // 获取按钮是空的，就无法执行点击"开红包"，怀疑是页面对应控件还没有加载处理，所以加了个延迟执行，如果加载这里
        // 会影响其他事件，所以用接口的形式，需要的地方可以自行加延迟，而不影响别的地方
        accessibilityBusiness.dealEvent(event,this,object : LuckyBagService.ViewGet{
            override fun getRootView(): AccessibilityNodeInfo? {
                return rootInActiveWindow
            }
        })
    }


    override fun onInterrupt() {}
    override fun onServiceConnected() {
        super.onServiceConnected()
        Log.e("测试无障碍id", "启动")
        // 设置过滤的包名，也可以放在对应的配置xml文件里面
        val info: AccessibilityServiceInfo = serviceInfo
        info.packageNames = arrayOf(WECHAT_PACKAGE_NAME, DING_TALK_PACKAGE_NAME)
        serviceInfo = info
    }
}

